package com.ronri.list.extendlist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cz.nongyetongv5.R;

import java.util.List;

/**
 * 文件：ams.java
 * 包名：com.ronri.list.extendlist;   --- 正式发布时需要修正
 * 说明：轮播组件
 * 作者：MS During
 * 日期：2017/5/6
 * 历史：
 */
public class ChildListAdapter extends BaseExpandableListAdapter {

    private Context _mContext;
    private LayoutInflater mInflater;

    /*** 分组数据 ***/
    private List<ChildItemElement> _mGroups;

    /***
     * 构造函数
     * @param context
     * @param group
     */
    public ChildListAdapter(Context context, List<ChildItemElement> group) {
        super();
        _mContext = context;
        _mGroups = group;
    }


    /***
     * 获取分组数量
     * @return
     */
    @Override
    public int getGroupCount() {
        return _mGroups.size();
    }

    /***
     * 获取子数量
     * @param groupPosition
     * @return
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return _mGroups.get(groupPosition).getSize();
    }

    /***
     * 获取分组名称
     * @param groupPosition
     * @return
     */
    @Override
    public Object getGroup(int groupPosition) {
        return _mGroups.get(groupPosition).getGroupName();
    }

    /***
     * 获取指定组的指定儿子
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return _mGroups.get(groupPosition).getChild(childPosition);
    }

    /***
     * 获取分组ID
     * @param groupPosition
     * @return
     */
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    /***
     * 获取分组儿子的ID
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    /***
     * 获取分组视图信息
     * @param groupPosition
     * @param isExpanded
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getGroupView(int groupPosition,
                             boolean isExpanded,
                             View convertView,
                             ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(_mContext);
            convertView = inflater.inflate(R.layout.vpatrolelement, null);
        }

        TextView title = (TextView) convertView.findViewById(R.id.patrol_title);
        title.setText(getGroup(groupPosition).toString());// 设置大组成员名称

        if (_mGroups.get(groupPosition).getSize() > 0) {
            ImageView image = (ImageView) convertView.findViewById(R.id.rightimg);// 是否展开大组的箭头图标
            if (isExpanded)// 大组展开时的箭头图标
                image.setImageResource(R.mipmap.icon_list_down);
            else
                // 大组合并时的箭头图标
                image.setImageResource(R.mipmap.icon_list_right);
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(_mContext);
            convertView = inflater.inflate(R.layout.vpatrolelement, null);
        }
        final TextView title = (TextView) convertView
                .findViewById(R.id.patrol_title);// 显示用户名

        title.setText(_mGroups.get(groupPosition).getGroupChild().get(childPosition).Title);

        ImageView icon = (ImageView) convertView.findViewById(R.id.rightimg);

        icon.setImageResource(R.mipmap.icon_left);

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}
